package com.zyc.service.impl;

import com.zyc.dao.RequestRepository;
import com.zyc.service.RequestService;
import com.zyc.vo.RequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

// 请求服务接口实现类
@Service
@Slf4j
public class RequestServiceImpl implements RequestService {

    // 注解请求信息数据库操作DAO
    @Autowired
    private RequestRepository requestRepository;

    @Override
    public RequestInfo save(RequestInfo requestInfo) {
        return requestRepository.save(requestInfo);
    }

//    @Override
//    public boolean removeByPerformId(String performId) {
//        return requestRepository.deleteByPerformId(performId) > 0;
//    }

    @Override
    public List<RequestInfo> getRequestInfoIPList() {
        List<Map<String, Object>> requestInfoList =  requestRepository.findRequestInfoIPList();
        List<RequestInfo> requestInfos = new ArrayList<>();
        log.info("请求信息数量是: " + requestInfoList.size());
        // 封装请求信息,符合前端要求数据
        for (Map<String, Object> map : requestInfoList) {
            RequestInfo ri = new RequestInfo();
            ri.setPerformId((String) map.get("performId"));
            ri.setIpAddress((String) map.get("ipAddress"));
            ri.setTotalCount(((BigInteger)map.get("totalCount")).intValue());

            requestInfos.add(ri);
        }

        return requestInfos;
    }


    @Override
    public List<RequestInfo> getRequestInfoTypeList() {
        List<Map<String, Object>> requestInfoList =  requestRepository.findRequestInfoTypeList();
        List<RequestInfo> requestInfos = new ArrayList<>();
        log.info("请求信息数量是: " + requestInfoList.size());
        // 封装请求信息,符合前端要求数据
        for (Map<String, Object> map : requestInfoList) {
            RequestInfo ri = new RequestInfo();
            ri.setPerformId((String) map.get("performId"));
            ri.setRequestType((String) map.get("requestType"));
            ri.setTotalCount(((BigInteger)map.get("totalCount")).intValue());

            requestInfos.add(ri);
        }

        return requestInfos;
    }
}
